import datetime
from applications.common.spider import WeChatPublic, ScauMathMessage
from applications.models import LatestTimeModel
import time

task_list = ['get_wx_articles', 'get_scau_ms_msgs', 'task4']


def get_tars(req):
    ans = []
    for tar in req:
        res = {
            "id": tar.id,
            "fakeid": tar.details,
            "name": tar.name,
            "category_id": tar.category_id
        }
        ans.append(res)
    return ans


def get_wx_articles(a, b):
    # 获取数据库中的微信公众号类的最新信息，求出需要爬取的总数，当前的fakeid，最新时间
    print(f'获取公众号文章任务_1_{a},{b},{datetime.datetime.now()}')
    req = LatestTimeModel.query.filter_by(category_id=1).all()
    tars = get_tars(req)
    w = WeChatPublic()
    w.get_cookies()
    out_of_time = w.check_login()
    if out_of_time == 1:
        pass
    else:
        total = len(tars)
        print("总计：" + str(total) + "个待爬取公众号")
        for tar in tars:
            w.reset_wxpublic_tar(tar)
            w.get_article_links()
            w.download_articles()
            w.save_article()
            time.sleep(300)
    w.save_logs()
    print("获取URL任务_1_结束")


def get_scau_ms_msgs(a, b):
    print(f'获取数学与信息学院通知任务_2_{a}{b}{datetime.datetime.now()}')
    req = LatestTimeModel.query.filter_by(category_id=2).all()
    tars = get_tars(req)
    ms = ScauMathMessage()
    total = len(tars)
    print("总计：" + str(total) + "个待爬取目标")
    for tar in tars:
        ms.reset_math_message_tar(tar)
        ms.get_msg_link()
        ms.download_msgs()
        ms.save_msgs()
        time.sleep(120)
    ms.save_logs()
    print("获取数学与信息学院通知任务_2_结束")


def task4(a, b):
    print(f'定时任务_4_{a}{b}{datetime.datetime.now()}')
